#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int n, m, s, q;
int vis[1000], mp[400][400];
void floyd(int k)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (mp[i][j] > mp[i][k] + mp[k][j])
            {
                mp[i][j] = mp[i][k] + mp[k][j];
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> s >> q;
    vis[s] = 1;
    memset(mp, 0x3f, sizeof mp);
    for (int i = 1; i <= n; i++)
    {
        mp[i][i] = 0;
    }
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        mp[u][v] = min(w, mp[u][v]);
    }
    floyd(s);
    while (q--)
    {
        int op, yy;
        cin >> op >> yy;
        if (op == 1)
        {
            if (!vis[yy])
            {
                floyd(yy);
            }
            vis[yy] = 1;
        }
        else
        {
            if (mp[s][yy] == 0x3f3f3f3f || vis[yy] == 0)
            {
                cout << "-1" << endl;
                continue;
            }
            else
            {
                cout << mp[s][yy] << endl;
                s = yy;
            }
        }
    }
    return 0;
}